﻿using System;
using System.Collections.Generic;
using AccelerateSensor.Model.Node;

namespace AccelerateSensor.Model.Gateway
{
    /// <summary>
    /// 网关信息
    /// </summary>
    public class GatewayInfo
    {
        /// <summary>
        /// 编号
        /// </summary>
        public string Uuid { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public int Type { get; set; }

        /// <summary>
        /// 网络地址
        /// </summary>
        public string Ip => CommonAttributeInfo?.Ip;

        /// <summary>
        /// 网络端口
        /// </summary>
        public int Port => CommonAttributeInfo?.TcpPort ?? 0;

        /// <summary>
        /// 节点数
        /// </summary>
        public int NodeCount => NodeList?.Count ?? 0;

        /// <summary>
        /// 公有属性信息
        /// </summary>
        public GatewayCommonAttributeInfo CommonAttributeInfo { get; set; }

        /// <summary>
        /// 私有属性信息
        /// </summary>
        public GatewayPrivateAttributeInfo PrivateAttributeInfo { get; set; }

        /// <summary>
        /// 节点列表
        /// </summary>
        public List<NodeInfo> NodeList { get; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }
        public DateTime LocalUpdateTime { get; set; }

        public string TypeString
        {
            get
            {
                var gatewayType = Enum.IsDefined(typeof(GatewayType), Type)
                    ? (GatewayType) Type
                    : GatewayType.Unknown;

                string typeString;
                switch (gatewayType)
                {
                    case GatewayType.Gw1278:
                        typeString = "WS-2002";
                        break;
                    case GatewayType.Gw1301:
                        typeString = Ip.Trim() == "100.100.100.100"
                            ? "WS-2010"
                            : "WS-2001";
                        break;
                    default:
                        typeString = "未知类型";
                        break;
                }

                return typeString;
            }
        }

        public GatewayInfo()
        {
            NodeList = new List<NodeInfo>();
        }

        public void UpdateInfo(GatewayInfo info)
        {
            Type = info.Type;
            UpdateTime = info.UpdateTime;

            if (info.CommonAttributeInfo != null)
            {
                CommonAttributeInfo = info.CommonAttributeInfo;
            }

            if (info.PrivateAttributeInfo != null)
            {
                PrivateAttributeInfo = info.PrivateAttributeInfo;
            }
        }
    }
}
